# -*- coding: utf-8 -*-

# $Id: project.py 8 2012-09-02 01:35:34Z alexis.roda.villalonga@gmail.com $

from random import choice

import django
try:
    from django.utils.crypto import get_random_string
except ImportError:
    # django < 1.4 does not provide 'get_random_string', fallback to a
    # less secure random function
    def get_random_string(length, valid_chars):
        return "".join([choice(valid_chars) for i in range(length)])

from dja_skel.lib.skeleton import Skeleton


class BaseDjangoProjectSkel(Skeleton):
    variables = ("prjname", )
    template_engine = "cheetah"
    description = """Base skeleton for projects.

    Computed variables
    ------------------
    * secret_key
    * django_version
    """

    def get_extra_context(self, context):
        super(BaseDjangoProjectSkel, self).get_extra_context(context)
        valid_chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
        context["secret_key"] = get_random_string(50, valid_chars)
        context["django_version"] = django.VERSION
        return context


class Django13Project(BaseDjangoProjectSkel) :
    name = "project13"
    skeleton_path = "templates/project13"
    summary = "Skeleton for a django <= 1.3 project"
    description = """Creates an skeleton for a django < 1.4 project.

    The 'settings.py' is split in three files:

    * settings.py: common settings. Imports 'settings_dev.py' or
      'settings_prod.py' depending on the value of the 'FLAVOR'
      environment variable.

    * settings_dev.py: settings for the development environment

    * settings_prod.py: settings for the production environment

    Markers
    -------
    * project/settings_dev.py_tmpl: -*- INSTALLED APPLICATIONS -*-
    * project/settings_prod.py_tmpl: -*- INSTALLED APPLICATIONS -*-
    * project/urls.py_tmpl: -*- APPLICATION URLS -*-
    """


class Django14Project(BaseDjangoProjectSkel) :
    name = "project14"
    skeleton_path = "templates/project14"
    summary = "Skeleton for a django >= 1.4 project"
    description = """Creates an skeleton for a django >= 1.4 project.

    The 'settings.py' is split in three files:

    * settings.py: common settings. Imports 'settings_dev.py' or
      'settings_prod.py' depending on the value of the 'FLAVOR'
      environment variable.

    * settings_dev.py: settings for the development environment

    * settings_prod.py: settings for the production environment

    Markers
    -------
    * project/settings_dev.py_tmpl: -*- INSTALLED APPLICATIONS -*-
    * project/settings_prod.py_tmpl: -*- INSTALLED APPLICATIONS -*-
    * project/urls.py_tmpl: -*- APPLICATION URLS -*-
    """

